package com.ruoyi.robot.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.robot.entity.InputText;
import com.ruoyi.robot.entity.Perception;
import com.ruoyi.robot.entity.RequestData;
import com.ruoyi.robot.entity.UserInfo;
import com.ruoyi.robot.mapper.MonMpaper;
import com.ruoyi.robot.service.RobotService;
import com.ruoyi.userlogin.pojo.RecordsEntity;
import com.ruoyi.utils.GetTimeutils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class RobotServiceImpl implements RobotService {

    @Value("${turing.url}")
    private String url;

    @Value("${turing.apiKey}")
    private String apiKey;

    @Value("${turing.userId}")
    private String userId;

    @Value("${turing.requestType}")
    private int requestType;


    /**
     * 注入一个mongodb层的操作对象
     */
    @Resource
    private MonMpaper monMpaper;


    // 向图灵接口发送数据
    public String sendData(String text) {
        // 设置请求头
        Map<String, String> header = new HashMap<>();
        // 字符集必须要设置为UTF-8
        header.put("Content-Type", "application/json;charset=UTF-8");
        //用户发送的消息
        String text1 = text;
        //用户输入的信息
        InputText inputText = new InputText();
        inputText.setText(text);

        Perception perception = new Perception();
        perception.setInputText(inputText);

        UserInfo userInfo = new UserInfo();
        //机器人的信息
        userInfo.setApiKey(apiKey);
        //用户的id
        userInfo.setUserId(userId);


        RequestData requestData = new RequestData();
        // 0 表示文本类型
        requestData.setReqType(requestType);
        requestData.setPerception(perception);
        requestData.setUserInfo(userInfo);
        //System.out.println(requestData+"RequestData==========================");
        Object o = JSONObject.toJSON(requestData);

        try {
            //用户成功发送消息后 把消息记录到mongoDB中
            RecordsEntity recordsEntity = new RecordsEntity();
            //记录用户发的信息
            String time = GetTimeutils.GetTime(new Date());
            recordsEntity.setPresenttime(time);
            recordsEntity.setUserId(userInfo.getUserId());
            recordsEntity.setMessage(text1);
            //记录用户的聊天信息
            monMpaper.save(recordsEntity);

            // response 为响应的内容
            String response = HttpRequest.post(url)
                    .headerMap(header, false)
                    .body(o.toString())
                    .timeout(30 * 1000)
                    .execute()
                    .body();

            return response;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // 获取图灵接口返回的文本信息
    public String getData(String userText) {
        String robotResponse = sendData(userText);
        JSONObject jsonObject = JSONObject.parseObject(robotResponse);
        JSONArray results = jsonObject.getJSONArray("results");
        JSONObject obj = (JSONObject)results.get(0);
        JSONObject values = obj.getJSONObject("values");
        String text = values.getString("text");
        System.out.println(text);
        //用户成功发送消息后 把消息记录到mongoDB中
        RecordsEntity recordsEntity = new RecordsEntity();
        //获取当前时间
        String time = GetTimeutils.GetTime(new Date());
        //记录机器人发的信息
        recordsEntity.setMessage(text);
        recordsEntity.setApiKey("智能小佑");
        recordsEntity.setPresenttime(time);
        //记录机器人发的信息
        monMpaper.save(recordsEntity);
        return text;
    }
}
